﻿using MySql.Data.MySqlClient;
using System.Data;

namespace MyBBSWebApi.Core
{
    public class MysqlHelper
    {
        public static string ConnectionString { get; set; } = "server=localhost;port=3306;user=root;password=123456; database=mybbs;";
        public static DataTable ExecuteTable(string cmdText, params MySqlParameter[] sqlParameters)
        {
            using MySqlConnection conn = new MySqlConnection(ConnectionString);
            conn.Open();
            MySqlCommand cmd = new MySqlCommand(cmdText, conn);
            cmd.Parameters.AddRange(sqlParameters);
            MySqlDataAdapter sda = new MySqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            return ds.Tables[0];
        }

        public static int ExecuteNonQuery(string cmdText, params MySqlParameter[] sqlParameters)
        {
            using MySqlConnection conn = new MySqlConnection(ConnectionString);
            conn.Open();
            MySqlCommand cmd = new MySqlCommand(cmdText, conn);
            cmd.Parameters.AddRange(sqlParameters);
            return cmd.ExecuteNonQuery();
        }
    }
}
